<?php
 
class Upload
{
    public      	$SavePath;
    public      	$ErrorReport;
    public  	$file = array();
    public	$FinalName;
    protected   	$AllowedExtensions = array();
    protected   	$SecurityPostName;
    protected   	$SecurityPostKey;
    protected   	$HiddenSecurity;
 
        public function __construct($path, $name, $extensions = "", $PostName ="", $PostKey = "")
        {
            $this->SavePath = $path;
            $this->file = $_FILES[$name];
 
            if ($extensions)
            {
                $this->AllowedExtensions = $this->ParseExtensions($extensions);
            }
            else
            {
                $this->AllowedExtensions = FALSE;
            }
 
            if($PostName AND $PostKey)
            {
                $this->SecurityPostKey = $PostKey;
                $this->SecurityPostName = $PostName;
                $this->HiddenSecurity = TRUE;
            }
            else
            {
                $this->HiddenSecurity = FALSE;
            }
 
            $this->ErrorManagement();
 
        }
 
        private function ParseExtensions($string)
        {
            $ExtensionsArray = explode(",", $string);
            return $ExtensionsArray;
        }
 
        private function FileExtention()
        {
            $exp = explode(".", $this->file['name']);
            $exp = array_reverse($exp);
            return $exp[0];
        }
 
 
        protected function ErrorManagement()
        {
            if ($this->HiddenSecurity)
            {
                if ($_POST[$this->SecurityPostName] != $this->SecurityPostKey)
                {
                    $this->ErrorReport = 14;
                    return FALSE;
                }
            }
 
            if ($this->AllowedExtensions)
            {
                if (!in_array($this->FileExtention(), $this->AllowedExtensions))
                {
                    $this->ErrorReport = 15;
                    return FALSE;
                }
            }
 
            switch($this->file['error'])
            {
                case UPLOAD_ERR_OK:
                $this->ErrorReport = 0;
                $this->SaveUploadFile();
                break;
 
                case UPLOAD_ERR_INI_SIZE:
                $this->ErrorReport = 16;
                break;
 
                case UPLOAD_ERR_FORM_SIZE:
                $this->ErrorReport = 17;
                break;
 
                case UPLOAD_ERR_PARTIAL:
                $this->ErrorReport = 18;
                break;
 
                case UPLOAD_ERR_NO_FILE:
                $this->ErrorReport = 19;
                break;
 
                case UPLOAD_ERR_NO_TMP_DIR:
                $this->ErrorReport = 20;
                break;
 
                case UPLOAD_ERR_CANT_WRITE:
                $this->ErrorReport = 21;
                break;
            }  
        }
 
        protected function SaveUploadFile()
        {
            if(is_uploaded_file($this->file['tmp_name']))
            {
                $this->FinalName = md5(microtime()) . '.' . $this->FileExtention();
				move_uploaded_file($this->file['tmp_name'], $this->SavePath . $this->FinalName);
                return TRUE;
            }
            else
            {
				$this->ErrorReport = 14;
                return FALSE;
			}
 
        }
}
 
 
 
?>